new Vue({
    el: '#app',
    data() {
        return {
            form: {
                SQLSerialNumber: "P_business_project1",
                PaginationNumber: 1,//页数
                RowAmount: 15,//每页数据条数
                Row_Status: '',
                Status: '',
                Activity_Name: '',
                Date: getYMDDate(),
                Condition: '',
                ExtensionCondition: '',
                Project_SerialNumber: '',
            },
            total: 0,
            tableData: [],	//表格数据
            Row_Status: [{
                label: '全部',
                value: '',
            }, {
                label: '已发布',
                value: '2',
            }, {
                label: '未发布',
                value: '0',
            }],
            Status: [{
                label: '全部',
                value: '',
            }, {
                label: '未发布',
                value: '1',
            }, {
                label: '未开始',
                value: '2',
            }, {
                label: '进行中',
                value: '3',
            }, {
                label: '已结束',
                value: '4',
            }],
            isTree: true,
            defaultProps: {
                children: 'Children',
                label: 'Project_Name'
            },
            treeData: [],
            visibleAddProject: false,
            visibleEditProject: false,
            dialogVisible: false,
            Project_SerialNumber: '',
            statistics: {
                date: '',
                month: '',
                year: '',
            },
            projectActivity: [],
            classify: [],
            form1: {
                value: [],
                business_article_classify: [],
                Date: [],
            }

        }
    },
    mounted() {
        this.getList(this.form);//前端分页
        this.getData();
    },
    computed: {
        Type() {
            return function (str1, str2) {
                let d1 = +new Date();
                let d2 = +new Date(str1)
                let d3 = +new Date(str2)
                if (d1 < d2) {
                    return '未开始'
                } else if (d2 < d1 && d1 < d3) {
                    return '进行中'
                } else if (d3 < d1) {
                    return '已结束'
                }
            }
        }
    },
    methods: {
        onSerach() {			//查询方法
            this.form.PaginationNumber = 1;
            this.getList(this.form)
        },
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
            this.getList(this.form);
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
            this.getList(this.form);
        },
        getData() {//查询项目
            const that = this;
            $.ajax({
                headers: requestHeaders1(),
                url: `${BASE_URL1}/projectActivity`,
                type: 'GET',
                success: function (res) {
                    that.projectActivity = res.Data;
                },
                dataType: "json"
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/getAllClassify`,
                type: 'GET',
                success: function (data) {
                    that.classify = data
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        getList(form) {//换页方法
            const that = this
            let data = deepClone(form),Condition='';
            if (data.Date) {
                data.Condition = window.btoa(unescape(encodeURIComponent(data.Date + "," + data.Date + "," + data.Date + "," + data.Date)))
            } else {
                data.Condition = window.btoa(unescape(encodeURIComponent(getYMDDate() + "," + getYMDDate() + "," + getYMDDate() + "," + getYMDDate())))
            }
            if (data.Date) {
                Condition = window.btoa(unescape(encodeURIComponent(data.Date + "," + data.Date + "," + data.Date)))
            } else {
                Condition = window.btoa(unescape(encodeURIComponent(getYMDDate() + "," + getYMDDate() + "," + getYMDDate())))
            }
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
                type: 'POST',
                data: {
                    SQLSerialNumber: "A_business_project2",
                    Condition,
                    ExtensionCondition: '',
                },
                success: function (data) {
                    that.statistics.date = data.filter(i => i.total == 'Date')[0].Collection_Amount;
                    that.statistics.month = data.filter(i => i.total == 'Month')[0].Collection_Amount;
                    that.statistics.year = data.filter(i => i.total == 'Year')[0].Collection_Amount;
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        Export() {
            const that = this;
            if (this.form1.Date.length > 0 && this.form1.value.length > 0 && this.form1.business_article_classify.length > 0) {
                // this.form1.Date[0]
                // let sql = "";
                // //
                // let str1 = "";
                // this.form1.value.forEach(item => {
                //     if (str1) {
                //         str1 = str1 + "OR business_project.Project_SerialNumber = '" + item[0] + "' AND business_activity.Activity_SerialNumber = '" + item[1] + "'\r\n";
                //     } else {
                //         str1 = "business_project.Project_SerialNumber = '" + item[0] + "' AND business_activity.Activity_SerialNumber = '" + item[1] + "'\r\n";
                //     }
                // })
                // sql = sql + "AND (" + str1 + ") \r\n";
                // //
                // let str2 = "";
                // this.form1.business_article_classify.forEach(item => {
                //     if (str2) {
                //         str2 = str2 + " OR business_activity.Classify_SerialNumber = '" + item[item.length - 1] + "' \r\n"
                //     } else {
                //         str2 = "business_activity.Classify_SerialNumber = '" + item[item.length - 1] + "' \r\n"
                //     }
                // })
                // sql = sql + " AND (" + str2 + ") \r\n";
                // //
                // sql = sql + "AND IFNULL(business_collection.Collection_Date,business_activity_article.Operation_Datetime) BETWEEN '" + this.form1.Date[0] + "' AND '" + this.form1.Date[1] + "'\r\n";
                // console.log(sql);
                $.ajax({
                    headers: requestHeaders1("JSON"),
                    type: 'POST',
                    url: `${BASE_URL1}/projectStatistics`,
                    // url: `http://127.0.0.1:8082/y/projectStatistics`,
                    data: JSON.stringify({
                        Date:this.form1.Date,
                        Classify:this.form1.business_article_classify,
                        Activity:this.form1.value
                    }),
                    success: function ({Data,Data2}) {
                        if (Data.length > 0) {
                            //创建xlsx结构
                            const wb = XLSX.utils.book_new();
                            const ws_data = [["项目名称", "活动名称", "一级分类", "二级分类", "三级分类", "日期", "日上架商品数据", "昨日上架商品数据","日上架商品数环比", "日销售额", "昨日销售额", "日销售额环比"]];
                            const ws_data1 = [["项目名称", "活动名称", "一级分类", "二级分类", "三级分类", "订单编号", "下单时间", "下单人姓名","收货人姓名", "联系方式", "收件地址", "商品名称","商品数量","销售单价","销售总价","供货单价","供货总价","供应商名称","发货状态","快递公司","快递单号","订单备注"]];
                            Data.forEach(item => {
                                ws_data.push([
                                    item.Project_Name,
                                    item.Activity_Name,
                                    item.Classify_Name.split('-')[0],
                                    item.Classify_Name.split('-')[1],
                                    item.Classify_Name.split('-')[2],
                                    item.Date,
                                    item.total,
                                    item.total1,
                                    item.total1>0?((item.total - item.total1)/item.total1*100).toFixed(2) +"%":"0.00%",
                                    parseFloat(item.Collection_Amount).toFixed(2),
                                    parseFloat(item.Collection_Amount1).toFixed(2),
                                    item.Collection_Amount1>0?((item.Collection_Amount - item.Collection_Amount1)/item.Collection_Amount1*100).toFixed(2) +"%":"0.00%",
                                ])
                            })
                            
                            Data2.forEach(item => {
                                ws_data1.push([
                                    item.Project_Name,
                                    item.Activity_Name,
                                    item.Classify_Name.split('-')[0],
                                    item.Classify_Name.split('-')[1],
                                    item.Classify_Name.split('-')[2],
                                    item.Sales_Order_SerialNumber,
                                    item.Sales_Date,
                                    item.Member_Nike,
                                    item.Sales_Contacts,
                                    item.Sales_Phone,
                                    item.Sales_Address,
                                    item.Article_Name,
                                    item.Quantity,
                                    item.Sales_Unit_Price,
                                    accMul(item.Quantity,item.Sales_Unit_Price),
                                    item.Purchase_Unit_Price,
                                    accMul(item.Quantity,item.Purchase_Unit_Price),
                                    item.Supplier_Name,
                                    item.Row_Status=='0'?'未发货':item.Row_Status=='1'?'部分发货':'已发货',
                                    item.Logistics_Name,
                                    item.Logistics_Number,
                                    item.Notes,
                                ])
                            })
                            const ws = XLSX.utils.aoa_to_sheet(ws_data);
                            ws['!cols'] = [];
                            ws_data[0].forEach(item => {
                                ws['!cols'].push({ alignment: { horizontal: "center" }, wpx: 150 });
                            })
                            const ws1 = XLSX.utils.aoa_to_sheet(ws_data1);
                            ws1['!cols'] = [];
                            ws_data1[0].forEach(item => {
                                ws1['!cols'].push({ alignment: { horizontal: "center" }, wpx: 150 });
                            })
                            XLSX.utils.book_append_sheet(wb, ws, "项目数据汇总");
                            XLSX.utils.book_append_sheet(wb, ws1, "订单数据汇总");
                            XLSX.writeFile(wb, '项目数据汇总' + (+new Date()) + ".xlsx");
                            that.$message({
                                message: '已导出项目数据汇总！',
                                type: 'success'
                            })
                        } else {
                            that.$message({
                                message: '暂无可导出的数据',
                                type: 'warning'
                            })
                        }
                    },
                    dataType: "json"
                })
                // $.ajax({
                //     headers: requestHeaders(),
                //     type: 'POST',
                //     url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumber`,
                //     data: {
                //         SQLSerialNumber: 'A_business_project3',
                //         Condition: '',
                //         ExtensionCondition: window.btoa(unescape(encodeURIComponent(sql))),
                //     },
                //     success: function (data) {
                //         if (data.length > 0) {
                //             //创建xlsx结构
                //             const wb = XLSX.utils.book_new();
                //             const ws_data = [["项目名称", "活动名称", "一级分类", "二级分类", "三级分类", "日期", "日上架商品数据", "日上架商品数环比", "日销售额", "日销售额环比"]];
                //             data.forEach(item => {
                //                 ws_data.push([
                //                     item.Project_Name,
                //                     item.Activity_Name,
                //                     item.Classify_Name.split('-')[0],
                //                     item.Classify_Name.split('-')[1],
                //                     item.Classify_Name.split('-')[2],
                //                     item.Date1?item.Date1:item.Date2,
                //                     item.total,
                //                     item.total1>0?((item.total - item.total1)/item.total1*100).toFixed(2) +"%":"0.00%",
                //                     item.Collection_Amount,
                //                     item.Collection_Amount1>0?((item.Collection_Amount - item.Collection_Amount1)/item.Collection_Amount1*100).toFixed(2) +"%":"0.00%",
                //                 ])
                //             })
                //             const ws = XLSX.utils.aoa_to_sheet(ws_data);
                //             ws['!cols'] = [];
                //             ws_data[0].forEach(item => {
                //                 ws['!cols'].push({ alignment: { horizontal: "center" }, wpx: 150 });
                //             })
                //             XLSX.utils.book_append_sheet(wb, ws, "项目数据汇总");
                //             XLSX.writeFile(wb, '项目数据汇总' + (+new Date()) + ".xlsx");
                //             that.$message({
                //                 message: '已导出项目数据汇总！',
                //                 type: 'success'
                //             })
                //         } else {
                //             that.$message({
                //                 message: '暂无可导出的数据',
                //                 type: 'warning'
                //             })
                //         }
                //     },
                //     complete: function (XMLHttpRequest) {
                //         requestPretreatment(XMLHttpRequest, false, (Status) => {
                //             if (Status == "error") {
                //                 that.$message.error("系统繁忙，请稍后再试");
                //             }
                //         });
                //     },
                //     dataType: "json"
                // })
            }else{
                this.$message({
                    message: '请先输入筛选条件',
                    type: 'warning'
                })
            }
        }
    },
})